package com.testapp1;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    /**
     *  跳转到 app2指定的activity  Manifests要设置
     *    android:exported="true"  才可以成功跳转
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void test(View view)
    {
        PackageInfo packageInfo;
        String packagename ="com.apptest2";
        try {
            packageInfo = getPackageManager().getPackageInfo(packagename, 0);
        } catch (Exception e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo != null) {
            //使用上面方法任意一种去开启App
            Toast.makeText(this, "已经安装app2", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            //B项目的包名, B项目要开启的Activity的包名
            ComponentName cn = new ComponentName("com.apptest2", "com.apptest2.MessageActivity");
            intent.setComponent(cn);
            intent.putExtra("data", "我是A的值");
            startActivity(intent);
        }else{
            Toast.makeText(this, "没安装该应用，可以去下载", Toast.LENGTH_SHORT).show();
        }


    }

}
